/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.edu.utp.gui;

import de.congrace.exp4j.UnknownFunctionException;
import de.congrace.exp4j.UnparsableExpressionException;
import java.awt.*;
import javax.swing.JFrame;
import net.ericaro.surfaceplotter.JSurfacePanel;
import net.ericaro.surfaceplotter.Mapper;
import net.ericaro.surfaceplotter.ProgressiveSurfaceModel;
import pl.edu.utp.calculations.RateFunction;

/**
 *
 * @author Erbi
 */
public class Chart {
    
    JSurfacePanel jsp = new JSurfacePanel();
    private ProgressiveSurfaceModel model;
    private RateFunction calculation;
    private String[] variables = {"x","y"};
    
    public void viewChart(final String function, final float result, int minValue, int maxValue) throws UnknownFunctionException, UnparsableExpressionException {
        JFrame jf = new JFrame("Wykres funkcji");
        jsp.setTitleText("3D Chart");
        jsp.setBackground(Color.WHITE);
        jsp.setTitleFont(jsp.getTitleFont().deriveFont(jsp.getTitleFont().getStyle() | Font.BOLD, jsp.getTitleFont().getSize() + 6f));
        jf.add(jsp, BorderLayout.CENTER);
        
        jsp.setConfigurationVisible(true);
        model = new ProgressiveSurfaceModel();
        jsp.setModel(model);
       
        calculation = new RateFunction(function, variables);

        
        model.setMapper(new Mapper() {
            @Override
            public  float f1(float x, float y)
            {
                    double[] values = {x,y};
                    float r = (float) calculation.calculate(values);

                    return (float)(r);
            }
                        
            @Override
            public  float f2(float x, float y)
            {
                    return result;
            }
        });
        
         model.plot().execute();
         
         model.setBothFunction(true);
         model.setBoxed(true);
         model.setDisplayXY(true);
         model.setDisplayZ(true);
         
         model.setXMax(maxValue);
         model.setXMin(minValue);
         model.setYMax(maxValue);
         model.setYMin(minValue);
         
        
         // get the screen size as a java dimension
         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

         // get 2/3 of the height, and 2/3 of the width
         int height = screenSize.height * 2 / 3;
         int width = screenSize.width * 2 / 3;

         // set the jframe height and width
         jf.setPreferredSize(new Dimension(width, height));
         jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         
         jf.pack();
         jf.setVisible(true);
    }

    
}
